﻿using System;
using System.Windows.Forms;
using GatewayInspect.Common;
using System.Data;
using System.Data.OracleClient;

namespace GatewayInspect.Modules
{
    public partial class FKModule : BaseModule
    {
        RFIDReader reader = null;

        public FKModule()
        {
            InitializeComponent();
        }

        public string TruckNo
        {
            get
            {
                return this.tbxTruckNo.Text;
            }
            set
            {
                this.tbxTruckNo.Text = value;
            }
        }

        public string HD_Weight
        {
            set
            {
                this.tbxHDWeight.Text = value;
            }
        }

        private string _EPC = "";

        private string _driverId = "";
        public string DriverID
        {
            get
            {
                return _driverId;
            }
            set
            {
                _driverId = value;
            }
        }

        protected override void OnInitialize()
        {
            base.OnInitialize();

            foreach (ToolStripItem item in this.tsMain.Items)
            {
                if (item != tsBtnClose)
                {
                    item.Visible = false;
                }
            }
        }

        protected override void OnInit()
        {
            base.OnInit();
            
            if (Session.IsSuper)
            {
                tbxCardNo.ReadOnly = false;
            }

            BeginRead();//连接读卡设备开始读卡
        }

        protected override void OnModuleDestroyed()
        {
            base.OnModuleDestroyed();
            if (reader != null)
            {
                reader.AfterReadCardInfo -= new RFIDReader.AfterReadCardInfoHandler(reader_AfterReadCardInfo);
                reader.CheckHandler();//检查是否还有其他对象读卡
            }
        }

        private void BeginRead()
        {
            if (reader == null)
            {
                reader = RFIDReader.GetInstance();
                reader.AfterReadCardInfo += new RFIDReader.AfterReadCardInfoHandler(reader_AfterReadCardInfo);
            }
            reader.BeginRead();
        }

        private void reader_AfterReadCardInfo(string strTid, string strEPC)
        {
            if (IsActive && !string.IsNullOrEmpty(strTid) && strTid != tbxCardNo.Text)
            {
                tbxCardNo.Text = strTid;
                this._EPC = strEPC;
            }
        }

        #region 发卡

        private void btnOK_Click(object sender, EventArgs e)
        {
            //int _sfgz = 1;//是否安全告知
            if (CheckInput())
            {
                string strTruckNo = this.tbxTruckNo.Text.Trim().ToUpper();
                string strCardNo = this.tbxCardNo.Text.Trim();
                DataTable dtRFID = DataAccess.ExecuteDataTable("select CARDID from RFID_READ where CARDSTATE = '0' and CHEHAO = '" + strTruckNo + "'");
                if (dtRFID != null && dtRFID.Rows.Count > 0)
                {
                    MessageBox.Show("该车已经发卡，卡号：" + dtRFID.Rows[0]["CARDID"].ToString());
                }
                else
                {
                    dtRFID = DataAccess.ExecuteDataTable("select CHEHAO from RFID_READ where CARDSTATE = '0' and CARDID = '" + strCardNo + "'");
                    if (dtRFID != null && dtRFID.Rows.Count > 0)
                    {
                        MessageBox.Show("该卡已经与车辆【" + dtRFID.Rows[0]["CHEHAO"].ToString() + "】关联！");
                    }
                    else
                    {
                        double _hdWeight = 0;
                        if (!string.IsNullOrWhiteSpace(tbxHDWeight.Text) && !double.TryParse(tbxHDWeight.Text.Trim(), out _hdWeight))
                        {
                            MessageBox.Show("车辆载质量非法！");
                            return;
                        }
                        string strSQL = "insert into RFID_READ(CARDID, CHEHAO, SENDCARDTIME, SENDCARDMAN, CARDSTATE, TIDID, HD_WEIGHT, ORGCODE)" +
                            " values (:CARDID, :CHEHAO, :SENDCARDTIME, :SENDCARDMAN, :CARDSTATE, :TIDID, :HD_WEIGHT, :ORGCODE)";
                        DataAccess.ExecuteNonQuery(strSQL,
                            new OracleParameter(":CARDID", strCardNo),
                            new OracleParameter(":CHEHAO", strTruckNo),
                            new OracleParameter(":SENDCARDTIME", DateTime.Now),
                            new OracleParameter(":SENDCARDMAN", Session.CurrentUserName),
                            new OracleParameter(":CARDSTATE", "0"),
                            new OracleParameter(":TIDID", _EPC),
                            new OracleParameter(":HD_WEIGHT", _hdWeight),
                            new OracleParameter(":ORGCODE", Session.CurrentOrgCode));

                        if (this.ParentForm is PopupForm)
                        {
                            this.ParentForm.DialogResult = DialogResult.OK;
                        }
                        else
                        {
                            MessageBox.Show("发卡成功！");
                        }
                    }
                }
            }
        }

        private bool CheckInput()
        {
            if (string.IsNullOrWhiteSpace(tbxTruckNo.Text) || string.IsNullOrWhiteSpace(tbxCardNo.Text))
            {
                MessageBox.Show("卡号、车牌号不能为空！");
                return false;
            }
            if (tbxTruckNo.Text.Trim().Length != 7)
            {
                MessageBox.Show("车牌号位数不正确！");
                return false;
            }
            return true;
        }

        //private bool CheckFKSZ(ref int _sfgz)
        //{
        //    string strSQL = "select * from T_SAFETY_INFORM where TRUCKNO = '" + TruckNo + "' and TRUNC(informtime) = TRUNC(sysdate)";
        //    DataTable dtInform = DataAccess.ExecuteDataTable(strSQL);
        //    if (dtInform == null || dtInform.Rows.Count <= 0)//未告知
        //    {
        //        _sfgz = 0;//未告知
        //        DataTable dtSetting = DataAccess.ExecuteDataTable("select WGZFK from T_SAFETY_INFORM_SETTING");
        //        if (dtSetting != null && dtSetting.Rows.Count > 0 && dtSetting.Rows[0]["WGZFK"].ToString() == "1")
        //        {
        //            return (MessageBox.Show("该车未进行安全告知确认，是否发卡？", "确认", MessageBoxButtons.YesNo) == DialogResult.Yes);
        //        }
        //        MessageBox.Show("该车未进行安全告知确认，不能发卡！");
        //        return false;
        //    }
        //    _sfgz = 1;//已告知
        //    return true;
        //}

        #endregion

        private void btnBeginRead_Click(object sender, EventArgs e)
        {
            BeginRead();
        }

        private void btnEndRead_Click(object sender, EventArgs e)
        {
            if (reader != null)
            {
                reader.EndRead();
            }
            MessageBox.Show("已停止读卡！");
        }

        /*
        private void btnQuery_Click(object sender, EventArgs e)
        {
            DataTable dtRFID = GetRFIDByCardNo();
            if (dtRFID != null)
            {
                cdgvFK.DataSource = GetRFIDByCardNo();
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            DataTable dtRFID = GetRFIDByCardNo();
            if (dtRFID != null && dtRFID.Rows.Count > 0)
            {
                DataRow row = dtRFID.Rows[0];
                using (FKModifyForm form = new FKModifyForm(row["CHEHAO"].ToString(), row["CARDID"].ToString(), row["HD_WEIGHT"].ToString(), row["SENDCARDTIME"].ToString()))
                {
                    if (form.ShowDialog() == DialogResult.OK)
                    {
                        MessageBox.Show("修改成功！");
                    }
                }
            }
        }

        private DataTable GetRFIDByCardNo()
        {
            if (!string.IsNullOrWhiteSpace(tbxCardNo.Text))
            {
                string strSql = "select CARDID,CHEHAO,SENDCARDTIME,SENDCARDMAN,HD_WEIGHT from RFID_READ where ORGCODE = '" + Session.CurrentOrgCode + "' and CARDSTATE = '0' and CARDID = '" + tbxCardNo.Text.Trim() + "'";
                return DataAccess.ExecuteDataTable(strSql);
            }
            else
            {
                MessageBox.Show("卡号不能为空！");
                return null;
            }
        }
        **/
    }
}
